package rent_car.Utils;

import rent_car.domain.CarBase;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.sql.*;

public class DBUtils {
    private static String driver;
    private static String ip;
    private static String port;
    private static String user;
    private static String password;
    private static String database;
    private static Connection conn;
    static{
        String xpath;
        SAXReader reader = new SAXReader();
        Document read = null;
        try {
            read = reader.read("DB.xml");
        } catch (DocumentException e) {
            e.printStackTrace();
            System.out.println("DB.xml文件加载失败");
        }
        Element root = read.getRootElement();
        xpath ="property[@current=1]/driver";
        driver=root.selectSingleNode(xpath).getText();

        xpath ="property[@current=1]/ip";
        ip=root.selectSingleNode(xpath).getText();

        xpath = "property[@current=1]/port";
        port = root.selectSingleNode(xpath).getText();

        xpath ="property[@current=1]/user";
        user = root.selectSingleNode(xpath).getText();

        xpath="property[@current=1]/password";
        password = root.selectSingleNode(xpath).getText();

        xpath ="property[@current=1]/database";
        database = root.selectSingleNode(xpath).getText();

        String url="jdbc:mysql://"+ip+":"+port+"/"+database+"?characterEncoding=utf8";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static void FindCarById(int carType) {
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM carbase AS cr,cartype AS ct\n" +
                    "WHERE cr.type=ct.type AND ct.id="+carType+"");
            while(rs.next()){
                System.out.println("id："+rs.getString("id"));
                System.out.println("    车牌："+rs.getString("name"));
                System.out.println("    价格："+rs.getString("price"));
                if(carType==1){
                    System.out.println("    载货量："+rs.getString("cSize"));
                }
                if(carType==2){
                    System.out.println("    载客量："+rs.getString("pSize"));
                }
                if(carType==3) {
                    System.out.println("    载货量：" + rs.getString("cSize"));
                    System.out.println("    载客量：" + rs.getString("pSize"));
                }
            }
            statement.close();
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //返回一个什么样的对象，增加一个对象可能会违背开闭原则，该用什么模式：工厂模式？
    public static CarBase GetCarById(int carid) throws SQLException {
        Statement statement = conn.createStatement();
        CarBase carBase = new CarBase();
        ResultSet rs = statement.executeQuery("select * from carbase where id=" + carid + "");
        double price=0.0;
        while(rs.next()){
            carBase.setPrice(rs.getDouble("price"));
            carBase.setName(rs.getString("name"));
            carBase.setcSize(rs.getString("cSize"));
            carBase.setpSize(rs.getString("pSize"));
        }
        return carBase;
    }
}
